package com.queen.restaurant.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {
	
	private static final int TIME_OUT = 10000;
	
	public static String getHttpGetResult(String url){
		CloseableHttpClient client = null;
		String result = "";
		
		try {
			client = HttpClients.createDefault();
			HttpGet get = new HttpGet(url);
			
			RequestConfig.Builder builder = RequestConfig.custom();
			builder.setConnectionRequestTimeout(TIME_OUT);
			RequestConfig config = builder.build();
			
			get.setConfig(config);
			
			HttpResponse response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				result = EntityUtils.toString(response.getEntity(), "utf-8");
			} else {
				result = "error";
			}
			
		} catch (ClientProtocolException e) {
			result = "error";
		} catch (IOException e) {
			result = "error";
		} finally {
			try {
				if (client != null) {
					client.close();
				}
			} catch (IOException e) {
				result = "error";
			}
		}
		
		return result; 
	}

	public static String getHttpPostResult(String url, Map<Object, Object> paramMap){
		CloseableHttpClient client = null;
		String result = "";
		try {
			client = HttpClients.createDefault();
			
			HttpPost post = new HttpPost(url);
			
			RequestConfig.Builder builder = RequestConfig.custom();
			builder.setConnectionRequestTimeout(TIME_OUT);
			RequestConfig config = builder.build();
			
			post.setConfig(config);
			
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			
			Iterator<Entry<Object, Object>> i = paramMap.entrySet().iterator();
			while (i.hasNext()) {
				Entry<Object, Object> entry = i.next();
//				System.out.println("key=" + entry.getKey().toString());
//				System.out.println("value=" + entry.getValue().toString());
				if (entry.getValue() == null) {
					list.add(new BasicNameValuePair(entry.getKey().toString(), ""));
				}else {
					list.add(new BasicNameValuePair(entry.getKey().toString(), entry.getValue().toString()));
				}
			}
			
			HttpEntity entity = new UrlEncodedFormEntity(list, Consts.UTF_8);
			post.setEntity(entity);
			
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				result = EntityUtils.toString(response.getEntity(), "utf-8");
			} else {
				result = "error";
			}
		} catch (ClientProtocolException e) {
			result = "error";
		} catch (IOException e) {
			result = "error";
		} finally {
			try {
				if (client != null) {
					client.close();
				}
			} catch (IOException e) {
				result = "error";
			}
		}
		return result;
	}
}
